package com.ibm.android;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;

import com.ibm.android.BusinessDataAdapter.BusinessDataAdapterListener;
import com.ibm.android.HistoryStore.Transaction;

public class HistoryView implements BusinessDataAdapterListener {
	ScanActivity mActivity;
	HistoryStore mStore;

	HistoryView(ScanActivity activity, HistoryStore store) {
		mActivity = activity;
		mActivity.setContentView(R.layout.history);
		mStore = store;

		init();
	}

	@Override
	public void onTouchName(Transaction item, int position) {
		Builder builder = new AlertDialog.Builder(mActivity);
		builder.setTitle("Product Information");
		// builder.setMessage("The amount of the product is: "
		// + item.getPaymentAmount() + "\n Date : "
		// + item.getSendPaymentOn());
		builder.setPositiveButton("OK", null);
		builder.show();
	}

	private void init() {
		// Set up the filter combobox.
		Spinner spinFilter = (Spinner) mActivity.findViewById(R.id.spinFilter);
		ArrayAdapter<CharSequence> filterAdapter = ArrayAdapter
				.createFromResource(mActivity, R.array.history_filters,
						android.R.layout.simple_spinner_item);
		filterAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinFilter.setAdapter(filterAdapter);

		// Set up the list views.
		final BusinessDataAdapter adapter = new BusinessDataAdapter(mActivity, mStore);
		adapter.addListener(this);

		ListView list = (ListView) mActivity.findViewById(R.id.ListView01);
		list.setAdapter(adapter);

		// Set up the buttons.
		Button button = (Button) mActivity.findViewById(R.id.btn_back);
		button.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				mActivity.initMain();
				return true;
			}
		});

		button = (Button) mActivity.findViewById(R.id.btn_clear);
		button.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				mStore.clear();
				adapter.notifyDataSetChanged();
				return true;
			}
		});
	}
}
